function  [equity_volatility, leverage, credit_spread, c, default_boundary, firm_value, equity_value, expropriation_benefits] = corpo_static(parameters)

%***********************************************************************
%
%        Static model endogenous agency conflicts, optimal debt, and
%        optimal default policy.
%
%***********************************************************************


% Part 1: Set the size of initial vector of variables 'theta'

    % Endogenous parameters are in theta = [coupon (c) boundary (xd) expropriation (b)]
    param_endo_0  = [0.5    0.24    0.01]';
    lb            = [0.001   0.01   0.0001]'; 
    ub            = [10.99   0.99  0.99]';


% Part 2: Optimization procedure
    options = optimset('Diagnostics','off','Display','off','MaxFunEvals',1000,'TolFun',1e-12,'TolCon',1e-12);
    [param_endo, ~] = fmincon(@dm_objfun,param_endo_0,[],[],[],[],lb,ub,@dm_spc,options,parameters);

% Part 3: Fixed parameters

    X0      = parameters.X0;        % Initial cash flow level
    X       = parameters.X;         % Date-t cash flow level
    sigma   = parameters.sigma;     % Cash flow volatility
    mu      = parameters.mu;        % Cash flow growth rate
    r       = parameters.r;         % Risk-free rate
    tau     = parameters.tau;       % Corporate tax rate
    alpha   = parameters.alpha;     % Liquidation cost in default
    psi     = parameters.psi;       % Manager's equity ownership
    theta   = parameters.theta;     % Degree of corporate governance

% PART 4: Get endogenous parameters

    c       = param_endo(1,1);           % Debt coupon
    Xd      = param_endo(2,1);           % Default barrier
    b       = param_endo(3,1);           % Expropriation benefits

% PART 5: Evaluate the model
    PI  = (1-tau)/(r-mu);
    PHI = (1-tau)/r;
    w   = 1/2 - mu/(sigma^2) - sqrt((1/2 - mu/(sigma^2))^2 + 2*r/(sigma^2));
        
    % Equity value
    E   = PI*(X0) - PHI*(b+c) - (PI*(Xd) - PHI*(b+c))*(X0/Xd)^w;
    
    % Unlevered firm value after default
    Au  = (1-alpha)*(PI*(Xd) - PHI*b); 
    
    % Debt and firm values
    D   = c/r - max(c/r - Au,0)*(X0/Xd)^w; 
    V   = E+D;
 
    % Objective function of the manager     
    R   = psi*V + [1-theta*b]*b/r; 
    y   = -R;
    
    % Asset value for current cash flow X 
    E   = PI*(X) - PHI*(b+c) - (PI*(Xd) - PHI*(b+c))*(X/Xd)^w;   
    D   = c/r - max(c/r - Au,0)*(X/Xd)^w; 
    V   = E+D;

    % Compute the first equity derivative for current cash flow X 
    if Xd == 0
    dEx = PI;
    else
    dEx = PI - (w/Xd)*(PI*(Xd) - PHI*(b+c))*[(X/Xd)^(w-1)];  
    end

% PART 6: Report values of interest
    expropriation_benefits  = b;
    default_boundary        = Xd;
    leverage                = (D/V)*100;
    credit_spread           = (c/D - r)*10000;
    equity_volatility       = 100*sigma.*X0*dEx/E;
    firm_value              = V;
    equity_value            = E;
    

%% Solve the model

function y = dm_objfun(param_endo,parameters)

% Fixed parameters

    X0      = parameters.X0;        % Initial cash flow level
    X       = parameters.X;         % Date-t cash flow level
    sigma   = parameters.sigma;     % Cash flow volatility
    mu      = parameters.mu;        % Cash flow growth rate
    r       = parameters.r;         % Risk-free rate
    tau     = parameters.tau;       % Corporate tax rate
    alpha   = parameters.alpha;     % Liquidation cost in default
    psi     = parameters.psi;       % Manager's equity ownership
    theta   = parameters.theta;     % Degree of corporate governance

% Get endogenous parameters

    c       = param_endo(1,1);           % Debt coupon
    Xd      = param_endo(2,1);           % Default barrier
    b       = param_endo(3,1);           % Expropriation benefits
    
% Evaluate the model

    PI  = (1-tau)/(r-mu);
    PHI = (1-tau)/r;
    w   = 1/2 - mu/(sigma^2) - sqrt((1/2 - mu/(sigma^2))^2 + 2*r/(sigma^2));
        
    % Equity value
    E   = PI*(X0) - PHI*(b+c) - (PI*(Xd) - PHI*(b+c))*(X0/Xd)^w;
    
    % Unlevered firm value after default
    Au  = (1-alpha)*(PI*(Xd) - PHI*b); 
    
    % Debt and firm values
    D   = c/r - max(c/r - Au,0)*(X0/Xd)^w; 
    V   = E+D;
 
    % Objective function of the manager     
    R   = psi*V + [1-theta*b]*b/r; 
    y   = -R;
    
    % Asset value for current cash flow X 
    E   = PI*(X) - PHI*(b+c) - (PI*(Xd) - PHI*(b+c))*(X/Xd)^w;   
    D   = c/r - max(c/r - Au,0)*(X/Xd)^w; 
    V   = E+D;

    % Compute the first equity derivative for current cash flow X 
    if Xd == 0
    dEx = PI;
    else
    dEx = PI - (w/Xd)*(PI*(Xd) - PHI*(b+c))*[(X/Xd)^(w-1)];  
    end
    
    
%% Optimal default boundary obtained from the smooth-pasting condition (SPC)

function [C,Ceq] = dm_spc(param_endo,parameters)

% Fixed parameters

    X0      = parameters.X0;        % Initial cash flow level
    X       = parameters.X;         % Date-t cash flow level
    sigma   = parameters.sigma;     % Cash flow volatility
    mu      = parameters.mu;        % Cash flow growth rate
    r       = parameters.r;         % Risk-free rate
    tau     = parameters.tau;       % Corporate tax rate
    alpha   = parameters.alpha;     % Liquidation cost in default
    psi     = parameters.psi;       % Manager's equity ownership
    theta   = parameters.theta;     % Degree of corporate governance

% Get endogenous parameters

    c       = param_endo(1,1);      % Debt coupon
    Xd      = param_endo(2,1);      % Default barrier
    b       = param_endo(3,1);      % Expropriation benefits
    

% Evaluate the model

    PI  = (1-tau)/(r-mu);
    PHI = (1-tau)/r;
    w   = 1/2 - mu/(sigma^2) - sqrt((1/2 - mu/(sigma^2))^2 + 2*r/(sigma^2));
        

% SPC at default

    % LHS : we evaluate model claims at X0 = Xd
    X0  = Xd;
    
    % Compute the first equity derivative
    if Xd == 0
    dEx = PI;
    else
    dEx = PI - (w/Xd)*(PI*(Xd) - PHI*(b+c))*[(X0/Xd)^(w-1)]; 
    end
    
    % RHS
    spc = 0;
    
    % Non linear constraints
    C   = [];
    Ceq = dEx - spc;
    
